package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.work.dto.BeastsBreedStockMessageDto;
import com.jiantong.work.impl.dao.BeastsBreedStockMessageDao;
import com.jiantong.work.model.BeastsBreedStockMessage;
import com.jiantong.work.service.BeastsBreedStockMessageReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据分析 - 存栏合计统计
 * Created by mz03 on 2017/4/17.
 */
@Slf4j
@Service
@RpcProvider
public class BeastsBreedStockMessageReadServiceImpl implements BeastsBreedStockMessageReadService{

    private final BeastsBreedStockMessageDao beastsBreedStockMessageDao;

    @Autowired
    public BeastsBreedStockMessageReadServiceImpl(BeastsBreedStockMessageDao beastsBreedStockMessageDao){
        this.beastsBreedStockMessageDao = beastsBreedStockMessageDao;

    }



    /**
     * 根据id
     *
     * @param id 主键id
     * @return BeastsBreedStockMessage 对象
     *
     * log.error("breedReport(id={}) is not found", id);
    return Response.fail("breedReport.not.found");

    log.error("failed to find breedReport(id={}), cause:{}", id, Throwables.getStackTraceAsString(e));
    return Response.fail("breedReport.find.fail");
     */
    @Override
    public Response<BeastsBreedStockMessage> findById(Long id) {
        try{
            BeastsBreedStockMessage beastsBreedStockMessage = beastsBreedStockMessageDao.findById(id);
            if (beastsBreedStockMessage == null){
                log.error("beastsBreedStockMessage (id = {}) is not found",id);
                return Response.fail("beastsBreedStockMessage.not.found");
            }
            return Response.ok(beastsBreedStockMessage);
        }catch(Exception e){
            log.error("failed to find beastsBreedStockMessage(id = {}),cause:{}",id, Throwables.getStackTraceAsString(e));
            return Response.fail("beastsBreedStockMessage.not.found");
        }
    }

    /**
     * 根据类型和指定天数 查找最新存栏合计总和
     *
     * @param map 包括类型和指定查找的天数
     * @return List<BeastsBreedStockMessage> 对象
     */
    @Override
    public Response<Map<Integer, Object>> findByType(Map<String,Object> map) {
        try{
            List<Long> list = beastsBreedStockMessageDao.findByType(map);
            if (list == null){
                log.error("find stock quantity by type(type = {}) is not found",map);
                return Response.fail("stock.quantity.not.found");
            }else {
                Map<Integer, Object> responseMap = new HashMap<>();
                for (int i = 0; i < list.size(); i++) {
                    responseMap.put(i, list.get(i));
                }
                return Response.ok(responseMap);
            }
        }catch (Exception e){
            log.error("find stock quantity by type(type = {}) is not found cause:{}",map,Throwables.getStackTraceAsString(e));
            return Response.fail("stock.quantity.not.found");
        }
    }
    public Response<Map<Integer, Object>> findByType1(Map<String,Object> map) {
        try{
            List<Long> list = beastsBreedStockMessageDao.findByType1(map);
            if (list == null){
                log.error("find stock quantity by type(type = {}) is not found",map);
                return Response.fail("stock.quantity.not.found");
            }else {
                Map<Integer, Object> responseMap = new HashMap<>();
                for (int i = 0; i < list.size(); i++) {
                    responseMap.put(i, list.get(i));
                }
                return Response.ok(responseMap);
            }
        }catch (Exception e){
            log.error("find stock quantity by type(type = {}) is not found cause:{}",map,Throwables.getStackTraceAsString(e));
            return Response.fail("stock.quantity.not.found");
        }
    }
    /**
     * 根据类型查询最近7天中 每天这种的合计信息中各个养殖场的存栏总和
     *
     * @param type type 养殖类型
     * @return Map<String,Long> 指定类型7天中每天的存栏合计 String: 从前往后的第几天;  Long: 当天的合计总和
     * <p>
     * select sum(stock_quantity) from djt_beasts_breed_stock_message
     * where breed_type = 1 group by date_format(change_date,'%y-%m-%d')
     * order by change_date desc
     * limit 7;
     */
    @Override
    public Response<Map<String, Long>> findByType7Day(Integer type) {
        try{
            List<Long> list = beastsBreedStockMessageDao.findByType7Day(type);
            if (list == null){
                log.error("find stock quantity by type(type = {}) is not found",type);
                return Response.fail("stock.quantity.not.found");
            }else {
                Map<String, Long> map = new HashMap<>();
                for (int i = 0; i <= list.size(); i++) {
                    map.put("day" + (i+1), list.get(i));
                }
                return Response.ok(map);
            }
        }catch (Exception e){
            log.error("find stock quantity by type(type = {}) is not found cause:{}",type,Throwables.getStackTraceAsString(e));
            return Response.fail("stock.quantity.not.found");
        }
    }

    /**
     * 一次查找最新7天中的每天的 所有种类(type)的最新存栏合计总和
     */
    @Override
    public Response<Map<String, BeastsBreedStockMessageDto>> findByType7Day() {
        try{
            return null;
        }catch (Exception e){
            return null;
        }
    }
}
